﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MiniCrawler.DataAccess.Repositories;
using MiniCrawler.Entity;
using MiniCrawler.Logging;

namespace MiniCrawler.Business.Collectors.ResourceCollectors
{
    public class WallpaperHdArranger
    {
        public void Run()
        {
            string resourcePath = "G:\\Downloads\\";
            string destinationPath = "G:\\ModCloth\\";

            var fileList = Directory.GetFiles(resourcePath);
            var filePath = string.Empty;
            var fileName = string.Empty;
            for(int i = 0; i < fileList.Count(); i ++)
            {

                filePath = fileList[i];
                fileName = Path.GetFileName(filePath);
                string url = "http://wallpaperswide.com/download/" + fileName;

                DownloadLink link = DownloadLinkHelper.GetLink(url);

                if(link != null)
                {
                    string folder = link.Path;
                    folder = destinationPath + folder.Replace("-gt;", "\\");

                    Log.WriteLog("[Create Folder][" + folder + "]");
                    Directory.CreateDirectory(folder);

                    Log.WriteLog("[Move file][" + fileName + "]");

                    if (!File.Exists(folder + "\\" + fileName))
                        File.Move(filePath, folder + "\\" + fileName);
                    else
                    {
                        if(File.Exists(filePath))
                            File.Delete(filePath);
                    }
                    Log.WriteLog("[Update file][" + fileName + "]");
                    Log.WriteLog("[Remain][" + i + "/" + fileList.Count() + "]");

                    link.IsDownloaded = true;
                    link.State = 3;
                    DownloadLinkHelper.UpdateLink(link);


                }
            }
        }
    }
}
